package spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.model.Car;
import spring.model.DataSource;
import spring.model.Person;
import spring.model.User;

/**
 * Administrator
 *
 * @author jiangbang
 * @date 2016/10/20
 */
public class Main {
    public static void main(String[] args) {
        test5();
    }

    private static void test1(){
        //创建User对象,并赋值
//        User user = new User();
//        user.setUsername("蒋棒");

        //1.创建Spring的IOC容器对象
        //ApplicationContext  代表IOC容器
        //ClassPathXmlApplicationContext：是ApplicationContext 接口的实现类，改实现类从类路径下来加载配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从IOC中获取Bean实例
        User user = (User) ctx.getBean("user");
//        User user = (User) ctx.getBean(User.class);
        //3,调用user方法
        System.out.println(user.getUsername());
    }

    /**
     * 通过构造函数注入
     */
    private static void test2(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Car car = (Car) ctx.getBean("car");
        Car car2 = (Car) ctx.getBean("car3");
        System.out.println(car.toString());
        System.out.println(car2.toString());
    }
    /**
     * 二个对象关系
     */
    private static void test3(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) ctx.getBean("person5");
        System.out.println(person.toString());
        //配置相关
        DataSource dataSource = ctx.getBean(DataSource.class);
        System.out.println(dataSource.getProperties());
    }

    /**
     * 自动装配
     */
    private static void test4(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
        Person person = (Person) ctx.getBean("person2");
        System.out.println(person.toString());
    }

    /**
     * 通过静态工厂方法启动
     */
    private static void test5(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
        Car person = (Car) ctx.getBean("car3");
        System.out.println(person.toString());
    }
}
